package com.ShottingWord.shared;

import java.util.ArrayList;

import com.google.gwt.user.client.Timer;
import com.google.gwt.user.client.Window;

public class StateChangerImpl implements StateChanger {
	
	private myTimer gametimer;
	
	/** 
	 * inner class for timer
	 * @author daniel
	 *
	 */
	class myTimer extends Timer	{

		private State state;

		public void setState(State state) {
			this.state = state;
		}
		
		@Override
		public void run() {
			state.setRemainingRound_time(state.getRemainingRound_time()-1000);
			state.setRemainingTotal_time(state.getRemainingTotal_time()-1000);
			checkTime(state);
			// Presenter should update the clock view here
		}	
	}	

	/**
	 * method for choosing the activeword from the problem set
	 * @param state
	 * @param word
	 */
	public void chooseWord(State state, String word){
		if (state.getWordList().contains(word))	{
			state.setActiveWord(word);
			//generate a new Pic choices 
			String[] source=state.getWordMap().getPicIdList();
			String[] temp=new String[state.getLevel()*Global.CHOICE_MULTIPLIER];
			for (int a=0;a<state.getLevel()*Global.CHOICE_MULTIPLIER;a++)	{
				temp[a]=source[(int)Math.random()*source.length-1];
			}
			state.setPic_Ids(temp);
			//Presenter should update the game view here (word list and choices)
		}
		else	{
			//possible exception
			Window.alert("word not found");
		}
		
	}
	
	/**
	 * This is basically makemove method, when a player choose the answer
	 */
	@Override
	public void shotPic(State state, String word, String pic) {
		//check if the word match with pic, if yes increase score, else penalty
		state.setTotalShot(state.getTotalShot()+1);
		if (state.checkPic(word,pic))	{
			state.setScore(state.getLevel()*Global.SCORE_MULTIPLIER+state.getScore());
			state.setAccuracy(state.getAccuracy()+1);
			removeWord(state);
		}
		else	{
			state.setRemainingRound_time(state.getRemainingRound_time() -
					(state.getLevel()*Global.PENALTY_TIME));
			state.setRemainingTotal_time(state.getRemainingTotal_time() -
					(state.getLevel()*Global.PENALTY_TIME));
			state.setScore(state.getScore() -
					(state.getLevel()*Global.PENALTY_SCORE));
		}
		checkTime(state);
		//Presenter should update game view here
	}
	
	/**
	 * void for removing the active word, i.e players answered that word already and move to the
	 * next word
	 */
	public void removeWord(State state)	{
		//remove the active word from the data structure in state
		ArrayList<String> temp=state.getWordList();
		temp.remove(state.getActiveWord());
		state.setWordList(temp);
		state.setActiveWord("");
		state.setRemainingWord_Num(state.getRemainingWordNum()-1);
		if (state.getRemainingWordNum()<=0)	{
			newRound(state);
		}
	}

	/**
	 * void for checking whether the player still have time and do necessary action
	 * @param state
	 */
	public void checkTime(State state)	{
		if (state.getRemainingTotal_time()<=0)	{
			gametimer.cancel();
			Window.alert(state.callResult().toString());
			//Presenter might have to do something here
		}
		else	if (state.getRemainingRound_time()<=0){
			newRound(state);
		}
	}
	
	/**
	 * void for start a new round
	 * @param state
	 */
	public void newRound(State state)	{
		state.setRemainingWord_Num(state.getLevel()*Global.WORD_MULTIPLIER);
		state.setRemainingRound_time(Global.ROUND_TIME);
		//generate a new set of wordlist from list ofkeys
		String[] source=state.getWordMap().getWordList();
		ArrayList<String>	temp=new ArrayList<String>();
		for (int a=0;a<state.getLevel()*Global.WORD_MULTIPLIER;a++)	{
			temp.add(source[(int)Math.random()*source.length-1]);
		}
		state.setWordList(temp);
		//Presenter should update the game view here
		
	}
	
	public void gameStart(State state)	{
		gametimer=new myTimer();
		gametimer.scheduleRepeating(1000);
		newRound(state);
	}
	

}
